home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue39 / mime / Base64.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-08-02  |  5.0 KB  |  152 lines

  1. unit Base64;
  2.  
  3. interface
  4. uses
  5.   Windows, Messages, SysUtils, Classes;
  6.  
  7. function encode(infilename: String; outfilename: String): boolean;
  8. function decode(infilename: String; outfilename: String): boolean;
  9.  
  10. implementation
  11.  
  12. function encode(infilename: String; outfilename: String): boolean;
  13. var
  14.   encodedString: String;
  15.   setof3ASCII: Array[1..3] of byte;
  16.   counter: Integer;
  17.   ord1,ord2,ord3: Integer;
  18.   file2enc: TFileStream;
  19.   read57: Array[1..57] of byte;
  20.   strlength: LongInt;
  21.   loops: Integer;
  22.   encodedfile: TStringList;
  23.   Base64Alphabet: String;
  24. begin
  25.   Base64Alphabet:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';;
  26.   file2enc:=TFileStream.Create(infilename,fmOpenRead);
  27.   encodedfile:=TStringList.Create;
  28.   strlength:=file2enc.Read(read57,57);
  29.   while strlength > 0 do
  30.   begin // beginning of main loop
  31.         encodedString:='';
  32.  
  33.         loops:=strlength div 3;
  34.         for counter:=0 to loops -1 do
  35.         begin
  36.             setof3ASCII[1]:=read57[counter*3 +1];
  37.             setof3ASCII[2]:=read57[counter*3 +2];
  38.             setof3ASCII[3]:=read57[counter*3 +3];
  39.         ord1:=ord(setof3ASCII[1]);
  40.         ord2:=ord(setof3ASCII[2]);
  41.         ord3:=ord(setof3ASCII[3]);
  42.                 encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
  43.             encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +(ord2 div 16)+1];
  44.             encodedString:=encodedString+base64Alphabet[(ord2 mod 16)*4 +(ord3 div 64)+1];
  45.             encodedString:=encodedString+base64Alphabet[ord3 mod 64+1];
  46.         end;
  47.         // two characters left over at end
  48.         if( strlength mod 3 = 2) then
  49.         begin
  50.             setof3ASCII[1]:=read57[strlength-1];
  51.             setof3ASCII[2]:=read57[strlength];
  52.         ord1:=ord(setof3ASCII[1]);
  53.         ord2:=ord(setof3ASCII[2]);
  54.                 encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
  55.             encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +(ord2 div 16)+1];
  56.             encodedString:=encodedString+base64Alphabet[(ord2 mod 16)*4 +1];
  57.                 encodedString:=encodedString+'=';
  58.         end;
  59.         // one character left over at end
  60.         if( strlength mod 3 = 1) then
  61.         begin
  62.             setof3ASCII[1]:=read57[strlength];
  63.         ord1:=ord(setof3ASCII[1]);
  64.                 encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
  65.             encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +1];
  66.                 encodedString:=encodedString+'=';
  67.                 encodedString:=encodedString+'=';
  68.         end;
  69.  
  70.         encodedfile.Add(encodedString);
  71.         strlength:=file2enc.Read(read57,57);
  72.      end; // end of main loop
  73.      encodedfile.SaveToFile(outfilename);
  74.      file2enc.Free;
  75.      Result:=true;
  76. end;
  77.  
  78. function decode(infilename: String; outfilename: String): boolean;
  79. var
  80.   stringToDecode: String;
  81.   setof4ASCII: Array[1..4] of char;
  82.   setof57: Array[1..57] of char;
  83.   maincounter, counter: Integer;
  84.   ord1,ord2,ord3, ord4: Integer;
  85.   use3rd, use4th: boolean;
  86.   outf: TFileStream;
  87.   total: Integer;
  88.   loops: Integer;
  89.   encodedfile: TStringList;
  90.   Base64Alphabet: String;
  91. begin
  92.    Base64Alphabet:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';;
  93.    encodedfile:=TStringList.Create;
  94.    encodedfile.LoadFromFile(infilename);
  95.    outf:=TFileStream.Create(outfilename,fmCreate);
  96.  
  97.    for maincounter:=0 to encodedfile.count - 1 do
  98.    begin // beginning of main loop
  99.         stringToDecode:=encodedfile.Strings[maincounter];
  100.         total:=0;
  101.         loops:=strlen(PChar(stringToDecode)) div 4;
  102.  
  103.         for counter:=0 to loops -1 do
  104.     begin
  105.             use3rd:=true;
  106.                 use4th:=true;
  107.  
  108.                    setof4ASCII[1]:=stringTodecode[counter*4 +1];
  109.             setof4ASCII[2]:=stringTodecode[counter*4 +2];
  110.             setof4ASCII[3]:=stringTodecode[counter*4 +3];
  111.             setof4ASCII[4]:=stringTodecode[counter*4 +4];
  112.  
  113.                 ord1:=pos(setof4ASCII[1],base64Alphabet)-1;
  114.         ord2:=pos(setof4ASCII[2],base64Alphabet)-1;
  115.                 if(setof4ASCII[3]='=') then
  116.                 begin
  117.                     ord3:=0;
  118.                         use3rd:=false;
  119.                 end
  120.                 else
  121.             ord3:=pos(setof4ASCII[3],base64Alphabet)-1;
  122.                 if(setof4ASCII[4]='=') then
  123.                 begin
  124.                     ord4:=0;
  125.                         use4th:=false;
  126.                 end
  127.                 else
  128.             ord4:=pos(setof4ASCII[4],base64Alphabet)-1;
  129.  
  130.              setof57[counter*3+1] :=chr(  ord1*4+(ord2 div 16) );
  131.                 if use3rd then
  132.                 begin
  133.              setof57[counter*3+2] :=chr( (ord2 mod 16)*16+(ord3 div 4) );
  134.                      inc(total);
  135.                 end;
  136.                 if use4th then
  137.                 begin
  138.              setof57[counter*3+3] :=chr( (ord3 mod 4)*64 +ord4 );
  139.                      inc(total);
  140.                 end;
  141.                 inc(total);
  142.         end;
  143.         if total > 0 then
  144.           for counter:=1 to total do
  145.                outf.Write(setof57[counter],1);
  146.      end; // end of main loop
  147.      outf.Free;
  148.      Result:=true;
  149. end;
  150.  
  151. end.
  152.